﻿using DataObjects;
using InfrastructureSqlServer.Helpers;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DataAccess
{
    public class ClienteDAO
    {
        private List<Cliente> CarregarListaFilial(SqlDataReader dr)
        {
            List<Cliente> liCliente = new List<Cliente>();

            while (dr.Read())
            {
                Cliente cliente = new Cliente();
                cliente.ClienteId = int.Parse(dr["CLIENTEID"].ToString());
                cliente.PessoaId = int.Parse(dr["PESSOAID"].ToString());
               
                liCliente.Add(cliente);
            }

            return liCliente;
        }

        public void Inserir(Cliente _cliente)
        {
            SqlParameter[] paramsToSP = new SqlParameter[1];

            paramsToSP[0] = new SqlParameter("@pessoaId", _cliente.PessoaId);

            try
            {
                SqlHelper.ExecuteScalar(ConfigurationManager.ConnectionStrings["conexao"].ToString(),
                                            CommandType.StoredProcedure, "stp_insert_cliente", paramsToSP);

            }
            catch (Exception e)
            {
                throw new Exception("Erro ao inserir cliente, Erro: " + e.Message);
            }

        }

        public bool Alterar(Cliente _cliente)
        {
            SqlParameter[] paramsToSP = new SqlParameter[2];

            paramsToSP[0] = new SqlParameter("@clienteId", _cliente.ClienteId);
            paramsToSP[1] = new SqlParameter("@pessoaId", _cliente.PessoaId);

            try
            {
                SqlHelper.ExecuteNonQuery(ConfigurationManager.ConnectionStrings["conexao"].ToString(),
                                            CommandType.StoredProcedure, "stp_update_cliente", paramsToSP);
                return true;
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao alterar cliente, Erro: " + e.Message);
            }

        }

        public bool Deletar(Cliente _cliente)
        {
            SqlParameter[] paramsToSP = new SqlParameter[1];

            paramsToSP[0] = new SqlParameter("@clienteId", _cliente.ClienteId);

            try
            {
                SqlHelper.ExecuteNonQuery(ConfigurationManager.ConnectionStrings["conexao"].ToString(),
                                            CommandType.StoredProcedure, "stp_delete_cliente", paramsToSP);
                return true;
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao deletar cliente, Erro: " + e.Message);
            }

        }

        public List<Cliente> Consultar(int _clienteId)
        {
            string sqlQuery = string.Format("SELECT * FROM CLIENTE WHERE CLIENTEID = {0}", _clienteId);

            try
            {
                return CarregarListaFilial(SqlHelper.ExecuteReader(ConfigurationManager.ConnectionStrings["conexao"].ToString(),
                                            CommandType.Text, sqlQuery));
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao consultar cliente, Erro: " + e.Message);
            }

        }

    }//class
}//namespace
